﻿angular.module("SurveyApp", []).
directive("centerDialog", function () {
    function centerElement(element) {
        element.css("left", ($(window).outerWidth(true) / 2) - (element.outerWidth(true) / 2))
            .css("top", ($(window).outerHeight(true) / 2) - (element.outerHeight(true) / 2))
            .focus();   
    }
    
    window.onresize = function () {
        $("[center-dialog]").each(function () {
            if ($(this).is(":visible")) centerElement($(this));
        });
    };
    
    return {
        link: function ($scope, element, attributes) {
            $scope.$watch(attributes.centerDialog, function (newValue, oldValue) {
                if (newValue) centerElement(element);
            });
        },
        restrict: "A"
    };
});


function SurveyCtrl($scope, $http) {
    
    $scope.RiskOptions = [{ text: "גבוהה מאוד", value: "5" }, { text: "גבוהה", value: "4" }, { text: "בינונית", value: "3" }, { text: "נמוכה", value: "2" }, { text: "נמוכה מאוד", value: "1"}];
    $scope.EnvironmentOptions = [{ text: "אפקטיבית מאוד", value: "1" }, { text: "אפקטיבית", value: "2" }, { text: "סבירה", value: "3" }, { text: "חלשה", value: "4" }, { text: "חלשה מאוד", value: "5"}];
    $scope.ComponentOptions = [{ text: "הפרדת תפקידים", value: "1" }, { text: "נהלים", value: "2" }, { text: "מדרג הרשאות", value: "3" }, { text: "ערוצי דיווח", value: "4" }, { text: "תמיכת מערכות מידע בתהליך", value: "5" }, { text: "הפקת לקחים", value: "6" }, { text: "בקרה מונעת", value: "7" }, { text: "מיומנות עובדים", value: "8"}];
    $scope.ReasonabilityOptions = [{ text: "גבוהה מאוד", value: "5" }, { text: "גבוהה", value: "4" }, { text: "בינונית", value: "3" }, { text: "נמוכה", value: "2" }, { text: "נמוכה מאוד", value: "1"}];

    $scope.waitMessage = "טוען נא להמתין";
    $http.post("ajax.aspx?action=getSurveyData").success(loadSurveyData);

    function loadSurveyData(response) {
        if (response) {
            if (response.message) $scope.message = response.message;
            if (response.data) {
                $scope.user = response.data.user;
                $scope.prevProcessID = response.data.prevProcessID;
                $scope.nextProcessID = response.data.nextProcessID;
                if (response.data.categories) $scope.CategoryOptions = response.data.categories;
                if (response.data.process) $scope.process = response.data.process;
            }

        }
        $scope.waitMessage = null;
    }

    $scope.closeDialog = function () {
        $scope.message = null;
    };

    $scope.prev = function () {
        $scope.waitMessage = "טוען סקר קודם";
        $http.post("ajax.aspx?action=goToProcess", { toProcessID: $scope.prevProcessID, process: $scope.process }).success(loadSurveyData);
    };

    $scope.save = function () {
        $scope.waitMessage = "שומר סקר";
        $http.post("ajax.aspx?action=saveProcess", $scope.process).success(function (response) {
            if (response) {
                if (response.message) $scope.message = response.message;
            }
            $scope.waitMessage = null;
        });
    };

    $scope.done = function () {
        
    };

    $scope.next = function () {
        $scope.waitMessage = "טוען סקר הבא";
        $http.post("ajax.aspx?action=goToProcess", { toProcessID: $scope.nextProcessID, process: $scope.process }).success(loadSurveyData);
    };
}

